package com.qf.android.huihui.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qf.android.huihui.R;
import com.qf.android.huihui.adapter.FragmentAdapter;
import com.qf.android.huihui.adapter.information.MenuAdapter1;
import com.qf.android.huihui.bean.show.MenuItemBean;
import com.qf.android.huihui.view.fragment.DetailListFragment;
import com.qf.android.huihui.view.fragment.DomesticFragment;
import com.qf.android.huihui.view.fragment.InformationFragment;
import com.qf.android.huihui.view.fragment.ShoppingFragment;
import com.qf.android.huihui.view.fragment.ShowFragment;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity implements AdapterView.OnItemClickListener {

    //晒物
    private ShowFragment showFragment;
    //资讯
    private InformationFragment informationFragment;
    //海淘
    private ShoppingFragment shoppingFragment;
    //国内
    private DomesticFragment domesticFragment;
    //清单
    private DetailListFragment detailListFragment;
    private ViewPager viewPager;
    //五个fragment
    private List<Fragment> fragment_list;
    //tab的五个名字
    private String[] arr;
    //
    private FragmentAdapter adapter;
    private TabLayout tabLayout;
    //抽屉布局
    private DrawerLayout drawerLayout;
    //抽屉布局左侧的listview
    private ListView draw_lv;
    //抽屉布局菜单分类名字
    private String[] classify_arr;
    //抽屉布局菜单实体类组成的集合
    private List<MenuItemBean> menu_list;
    //
    private MenuAdapter1 adapter1;
    //弹出抽屉布局的按钮
    private ImageView click_menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置顶部导航栏透明
        initWindow();
        init();

    }

    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    private void init() {
        drawerLayout = (DrawerLayout) findViewById(R.id.activity_main);
        draw_lv = (ListView) findViewById(R.id.draw_lv);
        click_menu = (ImageView) findViewById(R.id.click_menu);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        arr = getResources().getStringArray(R.array.tab);
        classify_arr = getResources().getStringArray(R.array.menu);

        fragment_list = new ArrayList<>();
        menu_list = new ArrayList<>();
        //添加菜单栏实体类对象
        setMenuBean();
        adapter1 = new MenuAdapter1(menu_list, this);

        adapter = new FragmentAdapter(getSupportFragmentManager(), fragment_list, arr);

        draw_lv.setAdapter(adapter1);
        draw_lv.setOnItemClickListener(this);
        //viewPager中的子元素初始化
        initFragment();
        //初始化小滑块
        initTabLayout();

        viewPager.setAdapter(adapter);
    }

    private void initTabLayout() {
        //设置标题的字体颜色，正常和选中
        tabLayout.setTabTextColors(getResources().getColor(R.color.tab_no_select), getResources().getColor(R.color.tab_select));
        //设置小滑块的高度
        tabLayout.setSelectedTabIndicatorHeight(1);
        //设置小滑块的颜色
        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.tab_select));

        //设置顶部导航的背景色
        tabLayout.setBackgroundColor(Color.WHITE);
        //设置关联的ViewPager对象
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                viewPager.setCurrentItem(tab.getPosition(), true);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initFragment() {
        showFragment = new ShowFragment();
        informationFragment = new InformationFragment();
        shoppingFragment = new ShoppingFragment();
        domesticFragment = new DomesticFragment();
        detailListFragment = new DetailListFragment();
        fragment_list.add(showFragment);
        fragment_list.add(informationFragment);
        fragment_list.add(shoppingFragment);
        fragment_list.add(domesticFragment);
        fragment_list.add(detailListFragment);
    }


    private void setMenuBean() {
        String s0 = classify_arr[0];
        String s1 = classify_arr[1];
        String s2 = classify_arr[2];
        String s3 = classify_arr[3];
        String s4 = classify_arr[4];
        String s5 = classify_arr[5];
        String s6 = classify_arr[6];
        String s7 = classify_arr[7];
        Bitmap bitmap0 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxqb);
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxrybh);
        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxspsx);
        Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxfsxb);
        Bitmap bitmap4 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxmzgh);
        Bitmap bitmap5 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxydjk);
        Bitmap bitmap6 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxsmjd);
        Bitmap bitmap7 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxmywj);

        Bitmap bitmap00 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxqb_bs);
        Bitmap bitmap11 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxrybh_bs);
        Bitmap bitmap22 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxspsx_bs);
        Bitmap bitmap33 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxfsxb_bs);
        Bitmap bitmap44 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxmzgh_bs);
        Bitmap bitmap55 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxydjk_bs);
        Bitmap bitmap66 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxsmjd_bs);
        Bitmap bitmap77 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_index_sxmywj_bs);

        MenuItemBean bean0 = new MenuItemBean(bitmap0, s0, bitmap00);
        MenuItemBean bean1 = new MenuItemBean(bitmap1, s1, bitmap11);
        MenuItemBean bean2 = new MenuItemBean(bitmap2, s2, bitmap22);
        MenuItemBean bean3 = new MenuItemBean(bitmap3, s3, bitmap33);
        MenuItemBean bean4 = new MenuItemBean(bitmap4, s4, bitmap44);
        MenuItemBean bean5 = new MenuItemBean(bitmap5, s5, bitmap55);
        MenuItemBean bean6 = new MenuItemBean(bitmap6, s6, bitmap66);
        MenuItemBean bean7 = new MenuItemBean(bitmap7, s7, bitmap77);

        menu_list.add(bean0);
        menu_list.add(bean1);
        menu_list.add(bean2);
        menu_list.add(bean3);
        menu_list.add(bean4);
        menu_list.add(bean5);
        menu_list.add(bean6);
        menu_list.add(bean7);

    }

    //导航栏按钮的监听事件
    public void click(View view) {
        switch (view.getId()) {
            case R.id.click_menu:
                drawerLayout.openDrawer(Gravity.LEFT);
                break;
            case R.id.search:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case R.id.shopping_card:
                startActivity(new Intent(this, ShoppingCartActivity.class));
                break;
            case R.id.personal:
                startActivity(new Intent(this, PersonalActivity.class));
                break;
            default:
                break;
        }

    }

    //listView的监听事件
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //padding原来8dp 点击后0dp
        for (int j = 0; j < adapterView.getChildCount(); j++) {
            View view1 = adapterView.getChildAt(j);
            if (j != i) {
                ImageView iv = (ImageView) view1.findViewById(R.id.menu_iv);
                iv.setImageBitmap(menu_list.get(j).getMap());
                TextView tv = (TextView) view1.findViewById(R.id.menu_tv);
                tv.setTextSize(15);
                iv.setPadding(33, 33, 33, 33);
            } else {
                ImageView iv = (ImageView) view1.findViewById(R.id.menu_iv);
                iv.setImageBitmap(menu_list.get(j).getMap2());
                TextView tv = (TextView) view1.findViewById(R.id.menu_tv);
                tv.setTextSize(22);
                iv.setPadding(10, 10, 10, 10);
            }

        }

        //清单分条点击触发效果
        switch (i) {
            case 0:
                click_menu.setImageResource(R.drawable.ic_index_sxqb);
                break;
            case 1:
                click_menu.setImageResource(R.drawable.ic_index_sxrybhclick);
                break;
            case 2:
                click_menu.setImageResource(R.drawable.ic_index_sxspsxclick);
                break;
            case 3:
                click_menu.setImageResource(R.drawable.ic_index_sxfsxbclick);
                break;
            case 4:
                click_menu.setImageResource(R.drawable.ic_index_sxmzghclick);
                break;
            case 5:
                click_menu.setImageResource(R.drawable.ic_index_sxydjkclick);
                break;
            case 6:
                click_menu.setImageResource(R.drawable.ic_index_sxsmjdclick);
                break;
            case 7:
                click_menu.setImageResource(R.drawable.ic_index_sxmywjclick);
                break;
        }
        drawerLayout.closeDrawer(Gravity.LEFT);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearDiskCache(); //清理磁盘缓存
        Glide.get(this).clearMemory(); //清理内存缓存
    }
}
